import tkinter as tk
from tkinter import messagebox
from datetime import datetime


def calculate_day_of_year():
    try:
        # 获取用户输入的年份、月份和日期
        year = int(entry_year.get())
        month = int(entry_month.get())
        day = int(entry_day.get())

        # 解析日期
        date = datetime(year, month, day)

        # 计算这一天是这一年的第几天
        day_of_year = date.timetuple().tm_yday

        # 显示结果
        messagebox.showinfo("结果", f"这一天是这一年的第 {day_of_year} 天")
    except ValueError:
        messagebox.showerror("错误", "请输入正确的年份、月份和日期")


# 创建主窗口
root = tk.Tk()
root.title("日期计算器")

# 创建标签和输入框
label_year = tk.Label(root, text="年份：")
label_year.grid(row=0, column=0, padx=10, pady=10)
entry_year = tk.Entry(root)
entry_year.grid(row=0, column=1, padx=10, pady=10)

label_month = tk.Label(root, text="月份：")
label_month.grid(row=1, column=0, padx=10, pady=10)
entry_month = tk.Entry(root)
entry_month.grid(row=1, column=1, padx=10, pady=10)

label_day = tk.Label(root, text="日期：")
label_day.grid(row=2, column=0, padx=10, pady=10)
entry_day = tk.Entry(root)
entry_day.grid(row=2, column=1, padx=10, pady=10)

# 创建按钮
button = tk.Button(root, text="计算", command=calculate_day_of_year)
button.grid(row=3, column=0, columnspan=2, pady=10)

# 运行主循环
root.mainloop()